﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeakView.service;
using LeakView.view.controls;
using LeakView.common;

namespace LeakView.misc.view.controls
{
    public partial class HistoryYLLL : UserControl
    {

        public HistoryYLLL()
        {
            InitializeComponent();
            historyChartDouble1.Title = "压力";
            historyChartDouble2.Title = "流量";
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (!this.historyTimeRanger1.IsValid)
            {
                MessageBox.Show("参数错误，起始时间必须小于结束时间！", "错误");
                return;
            }

            try
            {
                using (new LoadingStarter())
                {
                    LoadData(this.historyChartDouble1, "压力", MiscType.HeadYL, MiscType.TailYL);
                    LoadData(this.historyChartDouble2, "流量", MiscType.HeadLS, MiscType.TailLS);
                }
            }
            catch
            {
                MessageBox.Show("获取数据失败，请确定服务器是否正常！", "错误");
            }
        }

        private void LoadData(HistoryChartDouble chart, string title, MiscType type1, MiscType type2)
        {
            chart.clear();

            var date = this.historyTimeRanger1.Date;
            var data1 = MiscModal.me.LoadHistory(type1, date.Year, date.Month, date.Day);
            var data2 = MiscModal.me.LoadHistory(type2, date.Year, date.Month, date.Day);

            var start = historyTimeRanger1.StartTime;
            var end = historyTimeRanger1.EndTime;

            chart.Title = string.Format("{0} ({1} - {2})", title, start.ToString("yyyy-MM-dd HH:mm"), end.ToString("yyyy-MM-dd HH:mm"));
            chart.SetTimeRange(start, end);

            data1.ForEach(d =>
            {
                chart.appendValue(0, Tools.ConvertLongToDateTime(d.timestamp),
                                 MiscModal.me.GetDoubleFromMiscValue(d));

            });

            data2.ForEach(d =>
            {
                chart.appendValue(1, Tools.ConvertLongToDateTime(d.timestamp),
                                 MiscModal.me.GetDoubleFromMiscValue(d));

            });

        }
    }
}
